package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlDictDtlMapper;
import com.zl.autocode.dao.autocode.ZlDictMapper;
import com.zl.autocode.dto.autocode.dict.ReqDictAddDTO;
import com.zl.autocode.dto.autocode.dict.ReqDictDelDTO;
import com.zl.autocode.dto.autocode.dict.ReqDictDetailDTO;
import com.zl.autocode.dto.autocode.dict.ReqDictEditDTO;
import com.zl.autocode.dto.autocode.dict.ReqDictQueryDTO;
import com.zl.autocode.dto.autocode.dict.ReqDictStateDTO;
import com.zl.autocode.dto.autocode.dict.ResDictAddDTO;
import com.zl.autocode.dto.autocode.dict.ResDictDelDTO;
import com.zl.autocode.dto.autocode.dict.ResDictDetailDTO;
import com.zl.autocode.dto.autocode.dict.ResDictEditDTO;
import com.zl.autocode.dto.autocode.dict.ResDictQueryDTO;
import com.zl.autocode.dto.autocode.dict.ResDictStateDTO;
import com.zl.autocode.dto.autocode.dict.ResDictVO2;
import com.zl.autocode.entity.autocode.ZlDict;
import com.zl.autocode.entity.autocode.ZlDictDtl;
import com.zl.autocode.entity.autocode.po.ZlDictVo;
import com.zl.autocode.service.autocode.IDictService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 字典
 * @author ZL
 */
@Service("autocodeDictService")
public class DictServiceImpl extends AutocodeBaseManager implements IDictService {

    @Autowired
    private ZlDictMapper dictMapper;
    @Autowired
    private ZlDictDtlMapper dictDtlMapper;

    @Override
    public ResDictQueryDTO queryDictList(ReqDTO<ReqDictQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResDictQueryDTO resDTO = BeanUtil.copyProperties(page, ResDictQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getDictTitle())){
            reqData.setDictTitle("%"+reqData.getDictTitle()+"%");
        }
        if(StringUtils.isNotBlank(reqData.getDictType())){
            reqData.setDictType("%"+reqData.getDictType()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlDictVo> list = dictMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResDictVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResDictDetailDTO queryDictDetail(ReqDTO<ReqDictDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDetailDTO reqData = reqInfo.getReqData();
        ResDictDetailDTO res = new ResDictDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlDict view = dictMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResDictAddDTO insertDictInfo(ReqDTO<ReqDictAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictAddDTO reqData = reqInfo.getReqData();
        ResDictAddDTO res = new ResDictAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlDict record = new ZlDict();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setDictId(IdUtil.simpleUUID());
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("dictId",record.getDictId());
        //检查主键是否冲突
        if(dictMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        dictMapper.insertSelective(record);
        
        ZlDict keyInfo = dictMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDictEditDTO updateDictInfo(ReqDTO<ReqDictEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictEditDTO reqData = reqInfo.getReqData();
        ResDictEditDTO res = new ResDictEditDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("dictId",reqData.getDictId());
        ZlDict keyInfo = dictMapper.selectOneByMap(paramMap);
        String oldDictType = keyInfo.getDictType();
        
        ZlDict record = new ZlDict();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        dictMapper.updateByPrimaryKeySelective(record);
        if(!oldDictType.equals(record.getDictType())){
            paramMap.clear();
            paramMap.put("dictType",oldDictType);
            ZlDictDtl dtlRecord = new ZlDictDtl();
            dtlRecord.setDictType(record.getDictType());
            dictDtlMapper.updateByMapSelective(dtlRecord , paramMap);
        }
        paramMap.clear();
        paramMap.put("dictId",record.getDictId());
        keyInfo = dictMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDictDelDTO deleteDictInfo(ReqDTO<ReqDictDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictDelDTO reqData = reqInfo.getReqData();
        ResDictDelDTO res = new ResDictDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        ZlDict record = dictMapper.selectOneByMap(paramMap);
        paramMap.put("dictType", record.getDictType());
        if(dictDtlMapper.countExistKey(paramMap) > 0){
            throw new BizException(GlobalRespEnum.DELETION_NOT_ALLOWED);
        }
        
        dictMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResDictStateDTO updateDictState(ReqDTO<ReqDictStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDictStateDTO reqData = reqInfo.getReqData();
        ResDictStateDTO res = new ResDictStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlDict record = new ZlDict();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        dictMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("dictId",record.getDictId());
        ZlDict keyInfo = dictMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}